﻿using System;
using System.Windows;
using System.Windows.Input;
using Microsoft.ApplicationServer.Caching.AdminApi;

namespace MDCAdminTool.Commands
{
	public class GetCacheClusterHealth : AsyncCommandBindingBase
	{
		public GetCacheClusterHealth()
		{
			Command = Application.Current.Resources["GetCacheClusterHealth"] as ICommand;
		}

		protected override void OnExecuted(object sender, ExecutedRoutedEventArgs e)
		{
			var cluster = e.Parameter as CacheCluster;
			if (cluster == null)
			{
				var @base = e.Parameter as GroupBase;
				if (@base != null)
				{
					cluster = @base.Cluster;
				}
				else
				{
					return;
				}
			}

			cluster.UseCacheCluster();
			cluster.GetHealthStatus();
		}

		protected override void OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			var service = e.Parameter as ICacheCommand;

			if (service != null)
				if (service.Status == ServiceStatus.Up || service.Status == ServiceStatus.Starting)
					e.CanExecute = true;
		}
	}
}
